package com.example.zhibo.controller;

import com.example.zhibo.common.FfmpegUtil;
import com.example.zhibo.common.GlobalVariables;
import com.example.zhibo.common.Result;
import com.example.zhibo.entity.Broad;
import com.example.zhibo.mapper.BroadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class StreamController {
    @Autowired
    BroadMapper broadMapper;

    @PostMapping("/startStreaming")
    public Result startStreaming(
            @RequestParam("reslution") String reslution,
            @RequestParam("frame") String frame,
            @RequestParam("streamType") String streamType,
            @RequestBody Map<String, List> requestBody) {
        // 获取电脑的 UUID
        UUID uuid = UUID.randomUUID();
        String computerUUID = uuid.toString();
        System.out.println("Computer UUID: " + computerUUID);

        // 获取全局变量实例
        GlobalVariables instance = GlobalVariables.getInstance();
        // 获取全局变量的值
        String video = instance.getVideo();
        System.out.println(requestBody);
        List<String> appKey = requestBody.get("streamList");
//        for (String key : appKey) {
//            System.out.println("appKey: " + key);
//        }


//        测试专用
//        System.out.println("Reslution: " + reslution);
//        System.out.println("Frame: " + frame);
        if (streamType.equals("1")) {
            for (String stream : appKey) {
                Broad broad = new Broad();
                broad.setUuid(String.valueOf(uuid));
                broad.setPlatform(stream);
                broad.setWay("视频推流");
                broad.setVideoUrl(video);
                broadMapper.insert(broad);
            }
            FfmpegUtil.streamVideoToRTMP(video, appKey, reslution, frame);
        } else if (streamType.equals("2")) {
            for (String stream : appKey) {
                Broad broad = new Broad();
                broad.setUuid(String.valueOf(uuid));
                broad.setPlatform(stream);
                broad.setWay("摄像头推流");
                broad.setVideoUrl("摄像头");
                broadMapper.insert(broad);
            }
            FfmpegUtil.streamToRTMP(appKey, reslution, frame);
        } else if (streamType.equals("3")) {
            for (String stream : appKey) {
                Broad broad = new Broad();
                broad.setUuid(String.valueOf(uuid));
                broad.setPlatform(stream);
                broad.setWay("桌面推流");
                broad.setVideoUrl("桌面");
                broadMapper.insert(broad);
            }
            FfmpegUtil.streamDesktopToRTMP(appKey, reslution, frame);
        } else {
            return Result.fail();
        }

        return Result.suc();
    }


//    @PostMapping("/startStreaming")
//    public Result startStreaming(
//                                 @RequestParam("steamList") List<String> appKey,
////                                 @RequestParam("liveKey") String secretKey,
////                                 @RequestParam("livePlatform") String liveApp,
//                                 @RequestParam("reslution") String Reslution,
//                                 @RequestParam("frame") String frame,
//                                 @RequestParam("streamType") String streamType) {
////        List<String> appKey = new ArrayList<>();
////        appKey.add("rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_26495850_4460810&key=925a8432f5f08185d98868920588089c&schedule=rtmp&pflag=1");
////        appKey.add("rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_26788923_6551934&key=5ab53ee28a27e86b99ced9cfc2d51e7c&schedule=rtmp&pflag=1");
//        // 获取电脑的 UUID
//        UUID uuid = UUID.randomUUID();
//        String computerUUID = uuid.toString();
//        System.out.println("Computer UUID: " + computerUUID);
//        // 获取全局变量实例
//        GlobalVariables instance = GlobalVariables.getInstance();
//        // 获取全局变量的值
//        String video = instance.getVideo();
////        // 存储直播日志
////        Broad broad = new Broad();
////        broad.setUuid(computerUUID);
////        broad.setPlatform(appKey.get(0));
////        broad.setWay("摄像头推流");
////        broad.setVideoUrl("摄像头");
////        broadMapper.insertData(broad);
////        System.out.println("streamType: " + streamType);
////        System.out.println("video: " + video);
////        System.out.println("appKey: " + appKey);
////        System.out.println("secretKey: " + secretKey);
////        System.out.println("liveApp: " + liveApp);
//        System.out.println("Reslution: " + Reslution);
//        System.out.println("frame: " + frame);
////        System.out.println("Audio: " + Audio);
////        System.out.println("Video: " + Video);
//        if (streamType.equals("1") == true){
//            FfmpegUtil.streamVideoToRTMP(video,appKey, Reslution, frame);
//        } else if (streamType.equals("2") == true) {
//            FfmpegUtil.streamToRTMP(appKey, Reslution, frame);
//        } else if (streamType.equals("3") == true) {
//            FfmpegUtil.streamDesktopToRTMP(appKey, Reslution, frame);
//        } else {
//            return Result.fail();
//        }
//        return Result.suc();
//    }

//    @PostMapping("/startVideoStreaming")
//    public Result startVideoStreaming(
//            @RequestParam("liveKey") String secretKey,
//            @RequestParam("livePlatform") String liveApp,
//            @RequestParam("reslution") String Reslution,
//            @RequestParam("frame") String frame) {
//        // 获取全局变量实例
//        GlobalVariables instance = GlobalVariables.getInstance();
//        // 获取全局变量的值
//        String video = instance.getVideo();
//        System.out.println("video: " + video);
//        System.out.println("secretKey: " + secretKey);
//        System.out.println("liveApp: " + liveApp);
//        System.out.println("Reslution: " + Reslution);
//        System.out.println("frame: " + frame);
////        System.out.println("Audio: " + Audio);
////        System.out.println("Video: " + Video);
//        FfmpegUtil.streamVideoToRTMP(video,liveApp, secretKey, Reslution, frame);
//        String a = liveApp + secretKey;
//        return Result.suc(a);
//    }

    @PostMapping("/stopStreaming")
    public Result stopStreaming() {
        FfmpegUtil.stopStreaming();
        String b = "已停止推流";
        return Result.suc(b);
    }

    @PostMapping("/upload")
    public Result uploadVideo(@RequestPart("file") MultipartFile file) {
        try {
            // 检查文件夹是否已经存在
            File folder = new File("D:/zhibovideos");

            if (folder.exists()) {
                System.out.println("文件夹已存在");
            } else {
                // 创建文件夹
                boolean success = folder.mkdirs();
                if (success) {
                    System.out.println("文件夹创建成功");
                } else {
                    System.out.println("文件夹创建失败");
                }
            }
            // 视频保存路径
            String relativePath = "D:/zhibovideos";
            File directory = new File(relativePath);
            String uploadFolder = directory.getAbsolutePath()+"\\";
//            System.out.println(uploadFolder);

            // 生成文件名
            String fileName = file.getOriginalFilename();
//            System.out.println(fileName);

            // 构建目标文件对象
            File targetFile = new File(uploadFolder, fileName);

            // 如果文件已存在则删除
            if (targetFile.exists()) {
                targetFile.delete();
            }

            // 将上传的文件保存到目标位置
            FileOutputStream fileOutputStream = new FileOutputStream(targetFile);
            FileCopyUtils.copy(file.getInputStream(), fileOutputStream);
            // 获取全局变量实例
            GlobalVariables instance = GlobalVariables.getInstance();

            // 修改全局变量的值
            String filePath = instance.setVideo(uploadFolder + fileName);

            return Result.suc(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.fail();
        }
    }



}

